package com.cuitrip.business.home.trip.ui;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cuitrip.business.tripservice.ServiceDetailActivity;
import com.cuitrip.business.tripservice.TripEditActivity;
import com.cuitrip.business.tripservice.ui.IconTextView;
import com.cuitrip.business.tripservice.model.ServiceInfo;
import com.cuitrip.service.BuildConfig;
import com.cuitrip.service.R;
import com.cuitrip.util.PlatformUtil;
import com.cuitrip.util.ResourceUtil;
import com.cuitrip.util.time.TimeUtil;
import com.cuitrip.util.track.TrackUtil;
import com.lab.adapter.BaseHolderView;
import com.lab.adapter.IAdapterData;
import com.lab.utils.ImageHelper;
import com.lab.utils.MessageUtils;
import com.lab.utils.Utils;

import butterknife.ButterKnife;
import butterknife.Bind;


/**
 * 行程的holderview
 * Created by ak
 */
public class TripItemHolderView extends BaseHolderView {

    @Bind(R.id.service_img)
    ImageView ctServiceImg;
    @Bind(R.id.trip_status_text)
    TextView tripStatusText;
    @Bind(R.id.trip_publish_time)
    TextView tripPublishTime;
    @Bind(R.id.trip_status)
    IconTextView tripStatus;
    @Bind(R.id.trip_name)
    TextView ctServiceName;

    public TripItemHolderView(Context context) {
        super(context, R.layout.ct_finder_service_list_item);
    }

    @Override
    public void initView(View rootView) {
        this.setClickable(true);
        ButterKnife.bind(this);
    }

    @Override
    public void bindData(final IAdapterData data, final int position) {
        if (data instanceof ServiceInfo) {
            final ServiceInfo serviceItem = (ServiceInfo) data;
            ImageHelper.displayCtImage(serviceItem.getBackPic(), ctServiceImg, null);
            bindServiceInfo(serviceItem);
            if (serviceItem.isChecking()) {
                showChecking(serviceItem);
            } else if (serviceItem.isChecked()) {
                showChecked(serviceItem);
            } else if (serviceItem.isCheckFailed()) {
                showCheckFailed(serviceItem);
            } else if (serviceItem.isDraft()) {
                showDraft(serviceItem);
            } else {
                showError(serviceItem);
            }

            if (serviceItem.isCheckFailed() || serviceItem.isDraft()){
                if (BuildConfig.DEBUG){
                    setOnLongClickListener(new OnLongClickListener() {
                        @Override
                        public boolean onLongClick(View v) {
                            AlertDialog.Builder builder = MessageUtils.createHoloBuilder(getContext());
                            builder.setAdapter(new ArrayAdapter<String>(getContext(), R.layout.ct_choice_item, R.id.checktext,
                                    new String[]{getResources().getString(R.string.operation_delete), getResources().getString(R.string.operation_cancel)}), new Dialog.OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialogInterface, int position) {
                                    if (position == 0) {
                                        if (onDataChangeCallBack != null) {
                                            onDataChangeCallBack.onDataRemove(serviceItem);
                                        }
                                    } else {
                                        //do nothing
                                    }
                                }
                            });
                            builder.create().show();
                            return true;
                        }
                    });
                }
            }


            setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (serviceItem.getCheckStatus() == ServiceInfo.STATUS_CHECHED){
                        TrackUtil.trackScreenName("/insider/trips/view");
                        ServiceDetailActivity.startFinder(mContext, serviceItem.getSid(),serviceItem.getCheckStatus());
                    }else{
                        //由于这个接口取到的对象，只有extinfo，没有othernotes，所以做这个拷贝
                        TrackUtil.trackScreenName("/insider/trips/view");
                        serviceItem.copyExtInfo2OtherNotes();
                        TripEditActivity.navigate2TripEditActivity(getContext(),serviceItem,true);
                    }
                }
            });
        }
    }


    private String getFailedString(ServiceInfo serviceInfo) {
        if (serviceInfo != null && serviceInfo.getExtInfoObject() != null && !TextUtils.isEmpty(serviceInfo.getExtInfoObject().getRefuseReason())) {
            return serviceInfo.getExtInfoObject().getRefuseReason();
        }
        return PlatformUtil.getInstance().getString(R.string.trip_state_rejected);
    }

    private void bindServiceInfo(ServiceInfo serviceInfo) {
        ctServiceName.setText(serviceInfo.getName());
        String creationDate = Utils.getMsToD(serviceInfo.getGmtCreated());
        long l = TimeUtil.parseString2Long(creationDate, "yyyy-MM-dd");
       //tripPublishTime.setText(getResources().getString(R.string.trip_attribute_create_date) + creationDate);
        tripPublishTime.setText(getResources().getString(R.string.trip_attribute_create_date,TimeUtil.formatDay(tripPublishTime.getContext(),l)));
    }

    protected void showChecking(ServiceInfo serviceInfo) {
        tripStatusText.setTextColor(ResourceUtil.getThemeColor());
        tripStatusText.setText(getResources().getString(R.string.trip_state_waiting));

        tripStatus.setTextColor(getResources().getColor(R.color.mango_ffbb00));
        tripStatus.setText(getResources().getString(R.string.attention_icon));
    }

    protected void showDraft(ServiceInfo serviceInfo) {
        tripStatusText.setTextColor(ResourceUtil.getThemeColor());
        tripStatusText.setText(getResources().getString(R.string.trip_state_draft));

        tripStatus.setTextColor(getResources().getColor(R.color.mango_ffbb00));
        tripStatus.setText(getResources().getString(R.string.attention_icon));
    }


    protected void showChecked(ServiceInfo serviceInfo) {

        tripStatusText.setTextColor(getResources().getColor(R.color.ct_black));
        tripStatusText.setText(serviceInfo.getLikeNum());


        tripStatus.setTextColor(getResources().getColor(R.color.message_red_ff3b30));
        tripStatus.setText(getResources().getString(R.string.fav_full_icon));

    }

    protected void showCheckFailed(ServiceInfo serviceInfo) {

        tripStatusText.setTextColor(ResourceUtil.getThemeColor());
        tripStatusText.setText(getFailedString(serviceInfo));

        tripStatus.setTextColor(getResources().getColor(R.color.mango_ffbb00));
        tripStatus.setText(getResources().getString(R.string.attention_icon));

    }

    private OnDataChangeCallBack onDataChangeCallBack;

    public void setOnDataChangeCallBack(OnDataChangeCallBack onDataChangeCallBack) {
        this.onDataChangeCallBack = onDataChangeCallBack;
    }

    public interface OnDataChangeCallBack {

        public void onDataRemove(ServiceInfo serviceInfo);

    }

    protected void showError(ServiceInfo serviceInfo) {
        tripStatusText.setTextColor(ResourceUtil.getThemeColor());
        tripStatusText.setText(getResources().getString(R.string.feedback_data_err));

        tripStatus.setTextColor(getResources().getColor(R.color.mango_ffbb00));
        tripStatus.setText(getResources().getString(R.string.attention_icon));
    }


}
